<?php

    function getSOAPDate($timeStamp, $withTime = false)
    {
        $soapDate = date("Y-m-d", $timeStamp);
        return ($withTime) ? $soapDate."T".date("H:i:s", $timeStamp) : $soapDate."T00:00:00";
    }

    // $soap = new SoapClient("http://www.cbr.ru/DailyInfoWebServ/DailyInfo.asmx?WSDL");
    // $params["On_date"] = time(); //getSOAPDate(time());
    // var_dump($soap->GetCursOnDateXML($params));
    // echo cnv($soap->GetCursOnDateXML($params)->GetCursOnDateXMLResult->any);   

    function cnv($text)
    {
        return $text; //iconv("UTF-8", "WINDOWS-1251", $text);
    }

    function cnv_to($text)
    {
        return $text; //iconv("WINDOWS-1251", "UTF-8", $text);
    }

    function CallSoap($function, $param_xml, $server = null)
    {
        global $mikron;
        static $soap = null;
        static $soapserver = null;
        $parameters = $param_xml;
        unset($param_xml);
        if(!isset($_SESSION['soap']) || !isset($_SESSION['server']) || ($_SESSION['soap'] == null))
        {
            session_destroy();
            $mikron->Tools->GoToPage('index.php');
        }
        $server = $_SESSION['server'];
        if(is_null($soap) || ($server != $soapserver))
        {
            try
            {
                ini_set('default_socket_timeout', 3);
                $soap = new SoapClient($server, array("connection_timeout"=>3));
            }
            catch(Exception $ex)
            {
                $mikron->Tools->GoToPage('Location: /error.php?logout&serverproblem');
            }
            $soapserver = $server;
        }
        $params = Array();
        $params["MethodName"] = $function;
        $params["XML"] = $parameters;
        $params["SessionID"] = $mikron->Site->sessionid;

            /*$f=fopen("c:/0000.txt", "w");
            fwrite($f, $function."\r\n".$parameters);
            fclose($f);*/
        
        $resp = $soap->Operation($params);
        $respMethodName = $resp->MethodName;
        $respXML = $resp->XML;
        $resp = null;
        $soap = null;
        unset($resp);
        unset($soap);

        $respXML = str_replace('encoding="utf-16"', 'encoding="utf-8"', $respXML);

            /*$f=fopen("c:/1111.txt", "w");
            fwrite($f, $resp->MethodName."\r\n".$resp->XML);
            fclose($f);  */      

        if($respMethodName == 'error')
        {
            $_SESSION['error'] = $respXML;
            header('Location: error.php');
            exit;
        }
        elseif($respMethodName == 'authentification')
        {
            session_destroy();
            header("Location: index.php");
            exit;
        }

        $xml = $respXML;
        return $xml;

    }

?>
